package com.apexwear.apexutils.scan;

import android.text.TextUtils;
import android.util.Log;

import java.math.BigInteger;

public class ScanFilterUtils {
    final String TAG = this.getClass().getSimpleName();

    /**
     * 是否为自研手表
     *
     * @param broadcastByte
     * @return
     */
    public String isSelfWatch(String macAddress, byte[] broadcastByte) {
        if (broadcastByte == null) {
            return null;
        }
        boolean isCycle = true, isSelf = false;
        int dataLen = 0;
        byte[] signByte = null;
        while (isCycle) {
            //  Log.e(TAG, "数据角标:" + dataLen);
            if (dataLen >= broadcastByte.length - 1) {
                isCycle = false;
            } else {
                int startLen = broadcastByte[dataLen] & 0xFF;
                int startType = (broadcastByte[dataLen + 1]) & 0xFF;
                if (startType == 0xFF) {
                    if (startLen == 0x09) {
                        // Log.e(TAG, "所要查找的SN号");
                        signByte = new byte[startLen - 1];
                        System.arraycopy(broadcastByte, dataLen + 2, signByte, 0, signByte.length);
                        isSelf = true;
                        isCycle = false;
                    } else if (startLen == 0x0F) {
                        if (dataLen + 4 <= broadcastByte.length - 1) {
                            if ((broadcastByte[dataLen + 2] & 0xFF) == 0x4C && (broadcastByte[dataLen + 3] & 0xFF) == 0x53 && (broadcastByte[dataLen + 4] & 0xFF) == 0x3C) {
                                isSelf = true;
                                isCycle = false;
                            }
                        } else {
                            isCycle = false;
                        }
                    }
                    dataLen += (startLen + 1);
                } else if (startType == 0x03) {
                    if (startLen == 0x03) {
                        if (dataLen + 3 <= broadcastByte.length - 1) {
                            // Log.e(TAG, "第一个字节值:" + (bytes[dataLen + 2] & 0xFF) + ",第二个字节值:" + Integer.toHexString(bytes[dataLen + 3] & 0xFF));
                            if ((broadcastByte[dataLen + 2] & 0xFF) == 0x00 && (broadcastByte[dataLen + 3] & 0xFF) == 0x88) {
                                isSelf = true;
                                isCycle = false;
                            } else {
                                dataLen += (startLen + 1);
                            }
                        } else {
                            isCycle = false;
                        }
                    } else if (startLen == 0x05) {
                        if (dataLen + 5 <= broadcastByte.length - 1) {
                            if ((broadcastByte[dataLen + 2] & 0xFF) == 0x00 && (broadcastByte[dataLen + 3] & 0xFF) == 0x88 && (broadcastByte[dataLen + 4] & 0xFF) == 0x11 && (broadcastByte[dataLen + 5] & 0xFF) == 0x00) {
                                isSelf = true;
                                isCycle = false;
                            } else {
                                dataLen += (startLen + 1);
                            }
                        } else {
                            isCycle = false;
                        }
                    } else {
                        dataLen += (startLen + 1);
                    }
                } else {
                    dataLen += (startLen + 1);
                }
            }
        }
        if (isSelf) {
            if (signByte != null) {
                String oriData = "";
                for (int i = 0; i < signByte.length; i++) {
                    String hex = Integer.toHexString(signByte[i] & 0xFF);
                    oriData += (hex.length() < 2 ? "0" + hex : hex);
                }
                String sign = new BigInteger(oriData, 16).toString();
                if (oriData.length() > 18) {
                    sign = sign.substring(sign.length() - 18, sign.length());
                } else {
                    int size = 18 - sign.length();
                    if (size > 0) {
                        String pre = "";
                        for (int i = 0; i < size; i++) {
                            pre += "0";
                        }
                        sign = pre + sign;
                    }
                }
                return sign;
            } else {
                return macAddress;
            }
        }
        return macAddress;

    }

    /**
     * 是否为E03手表
     *
     * @param deviceName
     * @return
     */
    public boolean isE03Watch(String deviceName) {
        if (TextUtils.isEmpty(deviceName)) {
            return false;
        } else {
            if (deviceName.contains("E03")) {
                if (deviceName.contains("APEX")) {
                    return true;
                }
            }
            return false;
        }

    }

    public boolean isPolar(String deviceName) {
        if (TextUtils.isEmpty(deviceName)) {
            return false;
        }
        if (deviceName.contains("Polar")) {
            return true;
        }
        return false;
    }
}
